﻿using System;
using Microsoft.Build.Framework;
using Microsoft.TeamFoundation.Build.Proxy;
using Microsoft.TeamFoundation.Client;

namespace Codeplex.STSDTS.MSBuildTasks
{
    public abstract class BaseTask : ITask
    {


        private BuildStore _buildStore;

        protected BuildStore BuildStore
        {
            get
            {
                if (_buildStore == null)
                {
                    _buildStore = (BuildStore)Tfs.GetService(typeof(BuildStore));
                }
                return _buildStore;
            }
        }

        private TeamFoundationServer _tfs;

        protected TeamFoundationServer Tfs
        {
            get
            {
                if (_tfs == null)
                {
                    if (String.IsNullOrEmpty(TeamFoundationServerUrl))
                    {
                        // Throw some exception.
                    }
                    _tfs = TeamFoundationServerFactory.GetServer(TeamFoundationServerUrl);
                }
                return _tfs;
            }
        }

        /// <summary>
        /// The Url of the Team Foundation Server.
        /// </summary>
        [Required]
        public string TeamFoundationServerUrl
        {
            get;
            set;
        }

        /// <summary>
        /// The Url of the Build.
        /// </summary>
        [Required]
        public string BuildUri
        {
            get;
            set;
        }

        /// <summary>
        /// Logs given message to build log
        /// </summary>
        /// <param name="format">Format of the message</param>
        /// <param name="objects">Parameters of the message</param>
        public void Log(string format, params object[] objects)
        {
            BuildEngine.LogMessageEvent(
                new BuildMessageEventArgs(
                    string.Format(format, objects),
                    null,
                    GetType().Name,
                    MessageImportance.Normal)
                    );
        }

        public Guid CreateBuildStep(string format, params object[] objects)
        {
            var id = Guid.NewGuid();
            BuildStore.AddBuildStep(BuildUri, id.ToString(), string.Format(format, objects));
            return id;
        }

        public void EndBuildStep(Guid id, bool succeeded)
        {
            if (succeeded)
                BuildStore.UpdateBuildStep(BuildUri, id.ToString(), DateTime.Now, BuildStepStatus.Succeeded);
            else
                BuildStore.UpdateBuildStep(BuildUri, id.ToString(), DateTime.Now, BuildStepStatus.Failed);
        }

        #region ITask Members

        public IBuildEngine BuildEngine
        {
            get; set;
        }

        public abstract bool Execute();

        public ITaskHost HostObject
        {
            get; set;
        }

        #endregion
    }
}
